//http://plugins.jquery.com/project/autocenter
//http://stackoverflow.com/questions/210717/what-is-the-best-way-to-center-a-div-on-the-screen-using-jquery
(function($) {
	$.fn.extend( {
		center : function() {
			return this.each(function() {
				var top = 0;// ($(window).height() - $(this).outerHeight()) / 2;
					var left = ($(window).width() - $(this).outerWidth()) / 2;
					//alert("window "+$(window).width()+ " withdt "+$(this).outerWidth()+" left "+left);
					$(this).css( {
						position : 'absolute',
						margin : 0,
						top : (top > 0 ? top : 0) + 'px',
						left : (left > 0 ? left : 0) + 'px'
					});
				});
		}
	});
})(jQuery);
// jq-ingrid initialization
var maingrid;
$(window).load(function(){
	//$("#mainDiv").center(); 
  //  $("#mainDiv").center(); 
})

$(document).ready(function() {
	$(window).bind('resize', function() {
			$('#mainDiv').center( {
				transition : 300
			});
		});
		maingrid = $("#table1").ingrid( {
			url : 'BoardController',
			extraParams : {
				"getTable" : "true"
			},
			height : 380,
			initialLoad : false,
			colWidths : [ 20, 30, 100, 150, 150, 150 ],
			rowClasses : [ 'grid-row-style1', 'grid-row-style2' ],
			onRowSelect : rowSelect,
			onRowFocus : rowFocus,
			resizableCols : false,
			paging : false,
			sorting : false,
		});
		
			$('ul.jd_menu').jdMenu();
		
		$("#mainDiv").center(); 
	$("#mainDiv").center(); 
});



function rowSelect(tr, selected) {
	var str = selected ? 'SELECTED' : 'UNSELECTED';
	var trNumber = tr.cells.item(1).textContent || tr.cells.item(1).innerText;
	if (selected) {
		$('#select' + trNumber).attr('checked', true);
	} else {
		$('#select' + trNumber).attr('checked', false);
	}
}

function rowFocus(tr, selected) {
	var trNumber = tr.cells.item(1).textContent || tr.cells.item(1).innerText;
	$("#description").html(desc[trNumber]);
	if (trNumber in hist) {
		$("#history").html(hist[trNumber]);
	} else {
		$("#history").text("");
	}
}

function validateReserve(form) {
	var user = $('#user').val();
	var rows = maingrid.g.getSelectedRows();
	if (user == "") {
		jAlert('Please select user', 'Validation failed');
	} else if (rows.length == 0) {
		jAlert('Please select item', 'Validation failed');
	} else {
		form.submit();
	}
}

function validateUnreserve(form) {
	var rows = maingrid.g.getSelectedRows();
	if (rows.length == 0) {
		jAlert('Please select item', 'Validation failed');
	} else {
		form.submit();
	}
}
